#!/bin/bash

rootwookdir=$(pwd)
fbxTool=${rootwookdir}/"fbx-conv"
cczTool=${rootwookdir}/"ccz"

echo "copy particle began====================="
function scandirToCopyParticle() { 
    local cur_dir workdir  
    workdir=$1  
    cd ${workdir}  
    if [ ${workdir} = "/" ]  
    then  
        cur_dir=""  
    else  
        cur_dir=$(pwd)  
    fi  

    for dirlist in $(ls ${cur_dir})  
    do  
        if test -d ${dirlist};then  
            cd ${dirlist}  
            scandirToCopyParticle ${cur_dir}/${dirlist} $2  
            cd ..  
        else  
            local pathname=${cur_dir}/${dirlist}  
            if [[ ${pathname:(-${#2})} = $2 ]]   
            then  
                local file=$(basename ${pathname})
                local copyPath=${rootwookdir}/out/${file}
                $(cp ${pathname} ${copyPath})
            fi  
        fi  
    done 
}
scandirToCopyParticle "particle" ".pux"
cd .. 
echo "copy particle ended====================="

echo "convert mesh began====================="
function scandirToConvertMesh() { 
    local cur_dir workdir  
    workdir=$1  
    cd ${workdir}  
    if [ ${workdir} = "/" ]  
    then  
        cur_dir=""  
    else  
        cur_dir=$(pwd)  
    fi  

    for dirlist in $(ls ${cur_dir})  
    do  
        if test -d ${dirlist};then  
            cd ${dirlist}  
            scandirToConvertMesh ${cur_dir}/${dirlist} $2  
            cd ..  
        else  
            local pathname=${cur_dir}/${dirlist}  
            if [[ ${pathname:(-${#2})} = $2 ]]   
            then  
                local file=$(basename ${pathname} .FBX)
                local outfile=${rootwookdir}/out/mesh/${file}.c3b
                $(${fbxTool} -n 60 -b -l ${pathname} ${outfile})
            fi  
        fi  
    done 
}
scandirToConvertMesh "mesh" ".FBX"
cd .. 
echo "convert mesh ended====================="

echo "compress texture began====================="
function scandirToCompressTexture() { 
    local cur_dir workdir  
    workdir=$1  
    cd ${workdir}  
    if [ ${workdir} = "/" ]  
    then  
        cur_dir=""  
    else  
        cur_dir=$(pwd)  
    fi  

    for dirlist in $(ls ${cur_dir})  
    do  
        if test -d ${dirlist};then  
            cd ${dirlist}  
            scandirToCompressTexture ${cur_dir}/${dirlist} $2  
            cd ..  
        else  
            local pathname=${cur_dir}/${dirlist}  
            if [[ ${pathname:(-${#2})} = $2 ]]   
            then  
                $(${cczTool} ${pathname})
            fi  
        fi  
    done 
}
scandirToCompressTexture "texture" ".tga"
cd .. 
echo "compress texture ended====================="

echo "copy texture began====================="
function scandirToCopyTexture() { 
    local cur_dir workdir  
    workdir=$1  
    cd ${workdir}  
    if [ ${workdir} = "/" ]  
    then  
        cur_dir=""  
    else  
        cur_dir=$(pwd)  
    fi  

    for dirlist in $(ls ${cur_dir})  
    do  
        if test -d ${dirlist};then  
            cd ${dirlist}  
            scandirToCopyTexture ${cur_dir}/${dirlist} $2  
            cd ..  
        else  
            local pathname=${cur_dir}/${dirlist}  
            if [[ ${pathname:(-${#2})} = $2 ]]   
            then  
                local file=$(basename ${pathname})
                local copyPath=${rootwookdir}/out/texture/${file}
                $(cp ${pathname} ${copyPath})
            fi  
        fi  
    done 
}
scandirToCopyTexture "texture" ".png"
cd .. 
scandirToCopyTexture "texture" ".ccz"
cd .. 
echo "copy texture ended====================="

echo "convert sound began====================="
function scandirToConvertSound() { 
    local cur_dir workdir  
    workdir=$1  
    cd ${workdir}  
    if [ ${workdir} = "/" ]  
    then  
        cur_dir=""  
    else  
        cur_dir=$(pwd)  
    fi  

    for dirlist in $(ls ${cur_dir})  
    do  
        if test -d ${dirlist};then  
            cd ${dirlist}  
            scandirToConvertSound ${cur_dir}/${dirlist} $2  
            cd ..  
        else  
            local pathname=${cur_dir}/${dirlist}  
            if [[ ${pathname:(-${#2})} = $2 ]]   
            then  
                local file=$(basename ${pathname} .wav)
                local outfile=${rootwookdir}/out/sound/${file}.wav
                $(${afTool} -f 'WAVE' -d LEI16@22050 -c 1 ${pathname} ${outfile})
            fi  
        fi  
    done 
}
scandirToConvertSound "sound" ".wav"
cd .. 
echo "convert sound ended====================="
